#!/usr/bin/env python
"""
Acorn-SDR (A Collaboration of Radio Nodes)
(c) Bob Cowdery (G3UKB) 2009

File: gtk_filters.py
Date: April 2009

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

The author can be reached by email at

bob@g3ukb.co.uk

========================================================================
Module Description:

Manages the filters pop-up window

"""

#-----------------------------------------------------------------------
# System imports
import sys
from time import sleep
import subprocess

# Import GTK libraries
import pygtk
import gtk
import gtk.glade
import gobject

# Application imports
import utils

#======================================================================
# The Filters pop-up.
class FiltersGui:
  
    #---------------------------------------------------------------------
    def __init__(self, filters_tree, caps, dsp_pub, persist_service, callback):
        self.filters_tree = filters_tree
        self.caps = caps
        self.dsp_pub = dsp_pub
        self.persist_service = persist_service
        self.callback = callback
        self.ignore = False
        
        #-----------------------------------------------------------------------
        # Wire up the events
        dic =   {
            # Filters
            "on_tb_6k0_toggled" : self.filter_6k,
            "on_tb_2k4_toggled" : self.filter_2k4,
            "on_tb_2k1_toggled" : self.filter_2k1,
            "on_tb_1k0_toggled" : self.filter_1k,
            "on_tb_500_toggled" : self.filter_500,
            "on_tb_250_toggled" : self.filter_250,
            "on_tb_100_toggled" : self.filter_100,
            "on_tb_50_toggled" : self.filter_50,
            "on_b_cancel_filter_clicked" : self.cancel,
        }
        
        # This will connect up all the events in one hit
        self.filters_tree.signal_autoconnect (dic)
    
        # Now we need a reference to all the controls
        self.b6k = self.filters_tree.get_widget('tb_6k0')
        self.b2k4 = self.filters_tree.get_widget('tb_2k4')
        self.b2k1 = self.filters_tree.get_widget('tb_2k1')
        self.b1k = self.filters_tree.get_widget('tb_1k0')
        self.b500 = self.filters_tree.get_widget('tb_500')
        self.b250 = self.filters_tree.get_widget('tb_250')
        self.b100 = self.filters_tree.get_widget('tb_100')
        self.b50 = self.filters_tree.get_widget('tb_50')
        # Avoid circular events
        self.ignore = False
        
        self.w=self.filters_tree.get_widget('Filters')
        self.w.set_position(gtk.WIN_POS_MOUSE)
        
        # Widget maps
        self.dict = {'6K0':0,'2K4':1,'2K1':2,'1K0':3,'500':4,'250':5, '100':6, '50':7}
        self.widgets = [self.b6k, self.b2k4, self.b2k1, self.b1k, self.b500, self.b250, self.b100, self.b50]
        
        # Adjust for capabilities
        for widget in self.widgets:
            widget.set_sensitive(False)
        allowed_filters = self.caps.rsplit(',')
        for allowed_filter in allowed_filters:
            if allowed_filter in self.dict:
                self.widgets[self.dict[allowed_filter]].set_sensitive(True)
    
    #--------------------------------------------------------------------- 
    # Public Interface
    #---------------------------------------------------------------------
    def show_window(self):
        self.w.show_all()
    
    #---------------------------------------------------------------------
    def hide_window(self):
        self.w.hide()
        
    #---------------------------------------------------------------------
    # GUI generated events (i.e. things that happen when you play with widgets)
    # Events for the filter buttons
    #---------------------------------------------------------------------  
    def filter_6k(self, widget):
        if not self.ignore:
            #self.dsp_pub.setFilter('6K0')
            self.updater('6K0')
            self.w.hide()
  
    #---------------------------------------------------------------------  
    def filter_2k4(self, widget):
        if not self.ignore:
            #self.dsp_pub.setFilter('2K4')
            self.updater('2K4')
            self.w.hide()
  
    #---------------------------------------------------------------------  
    def filter_2k1(self, widget):
        if not self.ignore:
            #self.dsp_pub.setFilter('2K1')
            self.updater('2K1')
            self.w.hide()
    
    #---------------------------------------------------------------------  
    def filter_1k(self, widget):
        if not self.ignore:
            #self.dsp_pub.setFilter('1K0')
            self.updater('1K0')
            self.w.hide()
    
    #---------------------------------------------------------------------  
    def filter_500(self, widget):
        if not self.ignore:
            #self.dsp_pub.setFilter('500')
            self.updater('500')
            self.w.hide()
    
    #---------------------------------------------------------------------  
    def filter_250(self, widget):
        if not self.ignore:
            #self.dsp_pub.setFilter('250')
            self.updater('250')
            self.w.hide()
    
    #---------------------------------------------------------------------  
    def filter_100(self, widget):
        if not self.ignore:
            #self.dsp_pub.setFilter('100')
            self.updater('100')
            self.w.hide()
          
    #---------------------------------------------------------------------  
    def filter_50(self, widget):
        if not self.ignore:
            #self.dsp_pub.setFilter('50')
            self.updater('50')
            self.w.hide()
    
    #--------------------------------------------------------------------- 
    def updater(self, value):
        self.callback(value)
    
    #---------------------------------------------------------------------  
    def cancel(self, widget):
        self.w.hide()
    
    #---------------------------------------------------------------------  
    # Update events
    #---------------------------------------------------------------------  
    def filter_update(self, data):
        self.set_filter_hints(data)
    
    #---------------------------------------------------------------------
    # Private
    #---------------------------------------------------------------------             
    def set_filter_hints(self, filter):
        self.ignore = True
        for n in range(len(self.widgets)):
            if n == self.dict[filter]:
                self.widgets[n].set_active(True)
            else:
                self.widgets[n].set_active(False)
        self.ignore = False